<?php


  class TestAction extends CommonAction{
  
    public function fanyong(){
	 
	   
	   $orderno="r9p8ehgfg4";
	   
	   $order=M('buyer')->where('orderno="%s"',$orderno)->find();
	   
	   
	 //  dump($order['pid']);
	   
	   $mypianku=M('mypianku')->where('id="%s"',$order['pid'])->find();
	   
	 //  dump($mypianku);
	   
	   //这里用户
	   $user=M('dailiuser')->where('id="%s"',$mypianku['uid'])->find();
	   
	   
	  // dump($user);
	   
	   $yongjin=M('yongjin')->where('id=1')->find();  
	   
	   //如果有上级，第一层返利开始
	   if($user['upid']){
	   
	   $up1=M('dailiuser')->where('id="%s"',$user['upid'])->find();
	   
	   $yiji=$order['money']*($yongjin['yongjin1']/100);

	   $yiji  = round($yiji,2);
	   //加入用户余额
	   $data['money'] = $up1['money']+$yiji;
	   M('dailiuser')->where('id='.$user['upid'])->save($data);
	   //返佣记录写入数据库
	   $fan['fid'] = $order['uid'];
	   $fan['bid'] = $user['upid'];
	   $fan['money'] = $yiji;
	   $fan['time'] = time();
	   $fan['vid'] = $order['vid'];
	   $fan['ceng'] = 1;
	   M('fanli')->add($fan);

	   }
	   //如果一层有上级，第二层返利开始
	   if($up1['upid']){
	   
	   $up2=M('dailiuser')->where('id="%s"',$up1['upid'])->find();
	   
	 //  dump($up2);
	   
	   $erji=$order['money']*($yongjin['yongjin2']/100);

	   $erji  = round($erji,2);
	   //加入用户余额
	   $data1['money'] = $up2['money']+$erji;
	   M('dailiuser')->where('id='.$up2['id'])->save($data1);
	   //返佣记录写入数据库
	   $fan1['fid'] = $order['uid'];
	   $fan1['bid'] = $up1['upid'];
	   $fan1['money'] = $erji;
	   $fan1['time'] = time();
	   $fan1['vid'] = $order['vid'];
	   $fan1['ceng'] = 2;
	   M('fanli')->add($fan1);

	   }
	   //如果有二层有上级，第三层返利开始
	   if($up2['upid']){
	   
	   $up3=M('dailiuser')->where('id="%s"',$up2['upid'])->find();
	   
	   $sanji=$order['money']*($yongjin['yongjin3']/100);

	   $sanji  = round($sanji,2);
	   //加入用户余额
	   $data2['money'] = $up3['money']+$sanji;
	   M('dailiuser')->where('id='.$up3['id'])->save($data2);
	   //返佣记录写入数据库
	   $fan2['fid'] = $order['uid'];
	   $fan2['bid'] = $up2['upid'];
	   $fan2['money'] = $sanji;
	   $fan2['time'] = time();
	   $fan2['vid'] = $order['vid'];
	   $fan2['ceng'] = 3;
	   M('fanli')->add($fan2);

	   }
	    
	 
	   
	 
	 
	 
	 }
  
  
  
  }